package com.qf.controller;

import com.qf.service.DtsGoodsService;
import com.qf.service.DtsOrderService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/stat")
public class DtsAdminStatController {

    @Autowired
    DtsUserService userService;
    @Autowired
    DtsOrderService orderService;
    @Autowired
    DtsGoodsService goodsService;

    @GetMapping("/goods")
    public Object goods(){
        String[] columns = new String[]{"day","orders","products","amount"};
        List<Map<String,Object>> rows = goodsService.queryStatGoods();

        Map<String,Object> data = new HashMap<>();
        data.put("columns",columns);
        data.put("rows",rows);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/order")
    public Object order(){
        String[] columns = new String[]{"day","orders","customers","amount","pcr"};
        List<StatOrderVo> statOrderVos = orderService.queryStatOrder();

        Map<String,Object> data= new HashMap<>();
        data.put("columns",columns);
        data.put("rows",statOrderVos);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/user")
    public Object user(){
        String[] columns = new String[]{"day","users"};
        List<UserStatVo> userOrderCntVos = userService.queryStatUser();
        StatVo statVo = new StatVo();

        //第二种方法
//        List<Map> collect = userOrderCntVos.stream().map(userStatVo -> {
//            Map map = new HashMap();
//            String days = userStatVo.getDay();
//            Integer users = userStatVo.getUsers();
//            map.put("day", days);
//            map.put("users", users);
//            return map;
//        }).collect(Collectors.toList());
//        statVo.setRows(collect);

        //第一种方法
        for (UserStatVo UserStatVo : userOrderCntVos) {
            Map map = new HashMap();
            String days = UserStatVo.getDay();
            Integer users = UserStatVo.getUsers();
            map.put("day",days);
            map.put("users",users);
            statVo.add(map);
        }

        statVo.setColumns(columns);
        Map<String,Object> data = new HashMap<>();
        data.put("columns",statVo.getColumns());
        data.put("rows",statVo.getRows());
        return ResponseUtil.ok(data);
    }
}
